home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / SORT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-30  |  7.5 KB  |  354 lines

  1. #ifdef DEMO
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #ifdef  __STDC__
  6. #define x      x
  7. #else
  8. #define x      ()
  9. #endif
  10. #else
  11. #include "global.h"
  12. #include "commands.h"
  13. #include "hardware.h"
  14. #ifndef MSDOS
  15. #include "proc.h"
  16. #endif
  17. #endif
  18.  
  19. #ifdef __MSDOS__
  20. #undef MSDOS
  21. #define MSDOS 1
  22. #endif
  23.  
  24. #if !defined(_lint)
  25. static char rcsid[] OPTIONAL = "$Id: sort.c,v 1.19 1997/07/31 00:44:20 root Exp root $";
  26. #endif
  27.  
  28. #define ENDING ((size_t) -1)
  29.  
  30. int expired;
  31.  
  32. struct entries {
  33.     char *name;
  34.     long theindex;
  35.     size_t nextindex;
  36. };
  37.  
  38. void sortit (const char *fname,int entrysize, int searchsize, int strsize,time_t age);
  39.  
  40. static struct entries *e = ((struct entries *)0);
  41. static int count;
  42. static size_t current;
  43. static char name[16];
  44. static int result, same;
  45. static size_t theindex;
  46. static long currentindex;
  47. static int sortdups = 0, wassorted = 0;
  48. static size_t sorthead;
  49. static size_t lastindex;
  50.  
  51. static struct entries *temp = (struct entries *) 0;
  52. static struct entries *temp2 = (struct entries *) 0;
  53.  
  54.  
  55. static void search (void);
  56. static void insert (void);
  57. static void makesortname (char *buf, const char *origname);
  58.  
  59.  
  60. static void
  61. search ()
  62. {
  63.     same = 0;
  64.     lastindex = ENDING;
  65.     for (theindex = sorthead; theindex != ENDING; theindex = e[theindex].nextindex)        {
  66.         result = strnicmp (name, e[theindex].name, strlen(name));
  67.         if (!result)    {
  68.             same = 1;
  69.             sortdups++;
  70.             return;
  71.         }
  72.         if (result < 0)
  73.             return;
  74.         lastindex = theindex;
  75.     }
  76.     return;
  77. }
  78.  
  79.  
  80. static void
  81. insert ()
  82. {
  83.     temp = &e[current];
  84.     if (lastindex == ENDING)    {    /* at head of list */
  85.         temp->nextindex = sorthead;
  86.         sorthead = current;
  87.     } else     {    /* in the midst or tail of file */
  88.         temp2 = &e[lastindex];
  89.         if (theindex)        /* in midst of list */
  90.             temp->nextindex = temp2->nextindex;
  91.         temp2->nextindex = current;
  92.         if (!wassorted && !same && !theindex)
  93.             wassorted = 1;
  94.     }
  95.  
  96.     strcpy (temp->name, name);
  97. #ifdef DEMO
  98.     if (count)    {
  99.         if (temp->nextindex == ENDING)
  100.             printf ("inserting last record %s\n", temp->name);
  101.         else    {
  102.             temp2 = &e[e[current].nextindex];
  103.             printf ("inserting - %s -> %s\n", temp->name, temp2->name);
  104.         }
  105.     } else
  106.         printf ("inserting 1st record %s\n", temp->name);
  107. #endif
  108.     if (!same)
  109.         temp->theindex = currentindex;
  110.     else
  111.         temp->theindex = -1;
  112.     current++;
  113.     count++;
  114. }
  115.  
  116.  
  117. static void
  118. makesortname (buf, origname)
  119. char *buf;
  120. const char *origname;
  121. {
  122. char *cp;
  123.  
  124.     strcpy (buf, origname);
  125.     cp = strchr (buf, '.');
  126.     if (!cp)
  127.         cp = &buf[strlen(buf)];
  128.     strcpy (cp, ".srt");
  129. }
  130.  
  131.  
  132. void
  133. sortit (fname, entrysize, searchsize, strsize, date)
  134. const char *fname;
  135. int entrysize, searchsize, strsize;
  136. time_t date;
  137. {
  138. char *cp;
  139. unsigned k;
  140. FILE *fp, *out;
  141. char buf[128];
  142. long size0, size;
  143. #if 0
  144. int num;
  145. #endif
  146. time_t now, stamptime;
  147. int newnum = 0;
  148.  
  149. #ifdef DEMO
  150.     printf ("Filename: '%s', Entrysize=%d, Searchsize=%d\n", fname, entrysize, searchsize);
  151. #endif
  152.     if ((fp = fopen (fname, "rt")) == 0)    {
  153. #ifdef DEMO
  154.         printf ("can't open input file: %s\n", fname);
  155. #endif
  156.         return;
  157.     }
  158.     makesortname (buf, fname);
  159.     if ((out = fopen (buf, "wt")) == 0)    {
  160. #ifdef DEMO
  161.         printf ("can't open output file: %s\n", buf);
  162. #endif
  163.         (void) fclose (fp);
  164.         return;
  165.     }
  166.     size0 = (long) filelength(fileno(fp));
  167.     size = size0 / (long) entrysize;
  168.     size += 1;
  169. #ifdef DEMO
  170.     printf ("File length = %ld\n", size0);
  171.     printf ("%ld Entries found - Allocating structure...\n", size - 1);
  172. #else
  173.     log(-1,"Sort '%s' - %ld Entries originally", fname, size - 1);
  174. #endif
  175.     e = (struct entries *) mallocw ((unsigned)((unsigned long)size * sizeof(struct entries)));
  176.     for (current = 0; current < (size_t) size; current++)    {
  177.         e[current].name = (char *)mallocw ((unsigned)(searchsize + 1));
  178.         e[current].name[0] = 0;
  179.         e[current].nextindex = ENDING;
  180.         e[current].theindex = -1;
  181.         kwait (NULL);
  182.     }
  183.  
  184.     sorthead = ENDING;
  185.     current = count = 0;
  186.     expired = sortdups = wassorted = 0;
  187.     now = time(&now);
  188.     while (!feof (fp))    {
  189.         kwait (NULL);
  190.         currentindex = ftell(fp);
  191.         if ((long)current == size)
  192.             break;
  193.         (void) fgets (buf, entrysize, fp);
  194. #ifdef DEMO
  195.         printf ("Reading Entry %d/%d - ", current, count);
  196. #endif
  197.         if (feof (fp))    {
  198. #ifdef DEMO
  199.             putchar ('\n');
  200. #endif
  201.             continue;
  202.         }
  203.         kwait (NULL);
  204.         if (*buf > ' ' && ((int) strlen(buf) > (entrysize - 2)) && (*buf != '#'))    {
  205.             strncpy (name, buf, (unsigned)searchsize);
  206.             name[searchsize] = 0;
  207.             if ((cp = strpbrk (name, ".@ \t")) != 0)
  208.                 *cp = 0;
  209.             search();
  210.             kwait (NULL);
  211.             insert ();
  212.             kwait (NULL);
  213.         } else    {    /* skip it! */
  214. #ifdef DEMO
  215.             putchar ('\n');
  216. #endif
  217.             current++;
  218.             count++;
  219.             continue;
  220.         }
  221.         /* now check for expired date stamp, if used */
  222.         if (date)    {
  223.             cp = strchr (buf, ' ');
  224.             if (!cp)    {
  225.                 stamptime = 0;
  226.             } else {
  227.                 cp = skipwhite (cp);
  228.                 stamptime = atol(cp);
  229.             }
  230.             if ((stamptime == 0) || (now - stamptime >= date))    {
  231. #ifdef DEMO
  232.                 printf ("Expiring: now=%ld, age=%ld, stamptime=%ld\n", now, date, stamptime);
  233. #endif
  234.                 expired++;
  235.                 e[current].theindex = -2;
  236.             }
  237.         }
  238.     }
  239. #ifdef DEMO
  240.     putchar ('\n');
  241. #endif
  242.     (void) fflush (stdout);
  243.     if (expired || wassorted || sortdups)        {
  244.         for (theindex = sorthead,k=0; k < (unsigned)count && theindex != ENDING; k++,theindex = temp->nextindex)    {
  245.             temp = &e[theindex];
  246.             if (temp->theindex >= 0)    {
  247. #ifdef DEMO
  248.                 printf ("Outputing record %d/%d\n", k, count);
  249. #endif
  250.                 kwait (NULL);
  251.                 fseek (fp, (long) temp->theindex, 0);
  252.                 (void) fgets (buf, entrysize, fp);
  253.  
  254.                 if (strsize)    {
  255.                     /* we now re-format it, just in case it is bad */
  256.                     if ((cp = strpbrk (buf, " \t")) == 0)    {
  257. #ifdef DEMO
  258.                         printf ("Skipping malformed entry,,,,\n");
  259. #endif
  260.                         continue;
  261.                     }
  262.                     *cp++ = 0;
  263.                     cp = skipwhite (cp);
  264.                     stamptime = atol(cp);
  265.                     buf[strsize] = 0;    /* just in case */
  266.                     cp = buf;
  267.                     while (*cp)    {
  268.                         if (*cp & 0x80)    {
  269.                             *cp = 0;
  270.                             break;
  271.                         }
  272.                         cp++;
  273.                     }
  274.                     if (stamptime && *buf)
  275.                                 fprintf(out,"%-*s %-14ld\n",strsize,buf,stamptime);
  276.                     } else
  277.                     fputs (buf, out);
  278.                 newnum++;
  279.             }
  280. #ifdef DEMO
  281.             else     {
  282.                 if (temp->theindex == -2)
  283.                     printf ("Expiring record %d/%d\n", k, count);
  284.                 else printf ("Skipping record %d/%d\n", k, count);
  285.             }
  286. #endif
  287.             
  288.         }
  289.     }
  290. #ifndef DEMO
  291.     log(-1,"Sort '%s' - %d Entries at end", fname, newnum);
  292. #else
  293.     printf ("Sort '%s' - %d Entries at end\n", fname, newnum);
  294. #endif    
  295.     (void) fclose (fp);
  296.     (void) fclose (out);
  297.     for (current = 0; (long)current < size; current++)
  298.         free(e[current].name);
  299.     if (e != ((struct entries *)0))    {
  300.         free (e);
  301.         e = ((struct entries *)0);
  302.     }
  303.  
  304.     makesortname (buf, fname);
  305.     if (expired || wassorted || sortdups)    {
  306.         (void) remove (fname);
  307.         (void) rename (buf, fname);
  308.     } else
  309.         (void) remove (buf);
  310. }
  311.  
  312.  
  313.  
  314. #ifdef DEMO
  315. #define ENTRYSIZE 49
  316.  
  317. int
  318. kwait (i)
  319. int i;
  320. {
  321.     return i;
  322. }
  323.  
  324.  
  325. char *
  326. skipwhite (cp)
  327. char *cp;
  328. {
  329.     while (*cp && (*cp == ' ' || *cp == '\t'))
  330.         cp++;
  331.     return (cp);
  332. }
  333.  
  334.  
  335. void
  336. main (argc, argv)
  337. int argc;
  338. char *argv[];
  339. {
  340. unsigned long begincore;
  341.  
  342.     begincore = coreleft();
  343.     if (argc == 1)
  344.         sortit ("c:/nos/spool/wpagebbs", ENTRYSIZE, 6, 32, (time_t)0);
  345.     else
  346.         sortit (argv[1], atoi (argv[2]), atoi(argv[3]), atoi(argv[2]) - 17, (time_t) 0);
  347.     printf ("\nBeginning coreleft was %ld - Now is %ld\n", begincore, (long) coreleft());
  348.     if (!wassorted && !sortdups)
  349.         printf ("File was already sorted with no duplicates...\nNo further action needed\n");
  350.     else if (sortdups)
  351.         printf ("%d duplicates removed\n", sortdups);
  352. }
  353. #endif
  354.